<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>协程的案例 | 协程</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../chapter3/3_0.html" />
    
    
    <link rel="prev" href="../chapter1/1_0.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="2"
        data-chapter-title="协程的案例"
        data-filepath="chapter2/2_0.md"
        data-basepath=".."
        data-revision="Sat Oct 13 2018 10:54:16 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
                
                <li>
                    <a href="http://www.0voice.com" target="blank" class="custom-link">ntyTcp</a>
                </li>
            
            

            
            <li class="divider"></li>
            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        前言
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter1/1_0.html">
            
                
                    <a href="../chapter1/1_0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        协程的起源
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="2" data-path="chapter2/2_0.html">
            
                
                    <a href="../chapter2/2_0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        协程的案例
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3" data-path="chapter3/3_0.html">
            
                
                    <a href="../chapter3/3_0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        协程的实现之工作原理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4" data-path="chapter4/4_0.html">
            
                
                    <a href="../chapter4/4_0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        协程的实现之原语操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5" data-path="chapter5/5_0.html">
            
                
                    <a href="../chapter5/5_0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        协程实现之切换
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6" data-path="chapter6/6_0.html">
            
                
                    <a href="../chapter6/6_0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        协程实现之定义
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7" data-path="chapter7/7_0.html">
            
                
                    <a href="../chapter7/7_0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        协程实现之调度器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8" data-path="chapter8/8_0.html">
            
                
                    <a href="../chapter8/8_0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        协程性能测试
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9" data-path="chapter9/9_0.html">
            
                
                    <a href="../chapter9/9_0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                        协程多核模式
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >协程</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="2&#x534F;&#x7A0B;&#x7684;&#x6848;&#x4F8B;">2.&#x534F;&#x7A0B;&#x7684;&#x6848;&#x4F8B;</h1>
<p>&#x2003;&#x2003;<strong>&#x95EE;&#x9898;&#xFF1A;&#x534F;&#x7A0B;&#x5982;&#x4F55;&#x4F7F;&#x7528;&#xFF1F;&#x4E0E;&#x7EBF;&#x7A0B;&#x4F7F;&#x7528;&#x6709;&#x4F55;&#x533A;&#x522B;&#xFF1F;</strong></p>
<p>&#x2003;&#x2003;&#x5728;&#x505A;&#x7F51;&#x7EDC;IO&#x7F16;&#x7A0B;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x6709;&#x4E00;&#x4E2A;&#x975E;&#x5E38;&#x7406;&#x60F3;&#x7684;&#x60C5;&#x51B5;&#xFF0C;&#x5C31;&#x662F;&#x6BCF;&#x6B21;accept&#x8FD4;&#x56DE;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5C31;&#x4E3A;&#x65B0;&#x6765;&#x7684;&#x5BA2;&#x6237;&#x7AEF;&#x5206;&#x914D;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#xFF0C;&#x8FD9;&#x6837;&#x4E00;&#x4E2A;&#x5BA2;&#x6237;&#x7AEF;&#x5BF9;&#x5E94;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x3002;&#x5C31;&#x4E0D;&#x4F1A;&#x6709;&#x591A;&#x4E2A;&#x7EBF;&#x7A0B;&#x5171;&#x7528;&#x4E00;&#x4E2A;sockfd&#x3002;&#x6BCF;&#x8BF7;&#x6C42;&#x6BCF;&#x7EBF;&#x7A0B;&#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x5E76;&#x4E14;&#x4EE3;&#x7801;&#x903B;&#x8F91;&#x975E;&#x5E38;&#x6613;&#x8BFB;&#x3002;&#x4F46;&#x662F;&#x8FD9;&#x53EA;&#x662F;&#x7406;&#x60F3;&#xFF0C;&#x7EBF;&#x7A0B;&#x521B;&#x5EFA;&#x4EE3;&#x4EF7;&#xFF0C;&#x8C03;&#x5EA6;&#x4EE3;&#x4EF7;&#x5C31;&#x5475;&#x5475;&#x4E86;&#x3002;
&#x5148;&#x6765;&#x770B;&#x4E00;&#x4E0B;&#x6BCF;&#x8BF7;&#x6C42;&#x6BCF;&#x7EBF;&#x7A0B;&#x7684;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code>while (1) {
    int nfds = epoll_wait(epoll_fd, events, curfds, -1);
    if (nfds == -1) {
        perror(&quot;epoll_wait&quot;);
        break;
    }

    for (i = 0;i &lt; nfds;i ++) {
        int sockfd = listenfd(events[i].data.fd, sockfds);
        if (sockfd) {
            socklen_t len = sizeof(struct sockaddr_in);
            int clientfd = accept(sockfd, (struct sockaddr*)&amp;remote, &amp;len);

            pthread_t thread_id;
            pthread_create(&amp;thread_id, NULL, client_cb, &amp;clientfd);
        }
        else
        {
            ...
        }
}
</code></pre><p>&#x2003;&#x2003;&#x8FD9;&#x6837;&#x7684;&#x505A;&#x6CD5;&#xFF0C;&#x5199;&#x5B8C;&#x653E;&#x5230;&#x751F;&#x4EA7;&#x73AF;&#x5883;&#x4E0B;&#x9762;&#xFF0C;&#x5982;&#x679C;&#x4F60;&#x7684;&#x8001;&#x677F;&#x4E0D;&#x6253;&#x6B7B;&#x4F60;&#xFF0C;&#x4F60;&#x6765;&#x627E;&#x6211;&#x3002;&#x6211;&#x6765;&#x5E2E;&#x4F60;&#x8001;&#x677F;&#xFF0C;&#x4E3A;&#x6C11;&#x9664;&#x5BB3;&#x3002;<br>&#x2003;&#x2003;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x6709;&#x534F;&#x7A0B;&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x53EF;&#x4EE5;&#x8FD9;&#x6837;&#x5B9E;&#x73B0;&#x3002;&#x53C2;&#x8003;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF1A;<br>&#x2003;&#x2003;<a href="https://github.com/wangbojing/NtyCo/blob/master/nty_server_test.c" target="_blank">https://github.com/wangbojing/NtyCo/blob/master/nty_server_test.c</a></p>
<pre><code>while (1) {
    int nfds = epoll_wait(epoll_fd, events, curfds, -1);
    if (nfds == -1) {
        perror(&quot;epoll_wait&quot;);
        break;
    }

    for (i = 0;i &lt; nfds;i ++) {
        int sockfd = listenfd(events[i].data.fd, sockfds);
        if (sockfd) {
            socklen_t len = sizeof(struct sockaddr_in);
            int clientfd = accept(sockfd, (struct sockaddr*)&amp;remote, &amp;len);

            nty_coroutine *read_co;
            nty_coroutine_create(&amp;read_co, server_reader, &amp;clientfd);
        }
        else
        {
            ...
        }
}
</code></pre><p>&#x2003;&#x2003;&#x8FD9;&#x6837;&#x7684;&#x4EE3;&#x7801;&#x662F;&#x5B8C;&#x5168;&#x53EF;&#x4EE5;&#x653E;&#x5728;&#x751F;&#x6210;&#x73AF;&#x5883;&#x4E0B;&#x9762;&#x7684;&#x3002;&#x5982;&#x679C;&#x4F60;&#x7684;&#x8001;&#x677F;&#x8981;&#x6253;&#x6B7B;&#x4F60;&#xFF0C;&#x4F60;&#x6765;&#x627E;&#x6211;&#xFF0C;&#x6211;&#x5E2E;&#x4F60;&#x628A;&#x4F60;&#x8001;&#x677F;&#x6253;&#x6B7B;&#xFF0C;&#x4E3A;&#x6C11;&#x9664;&#x5BB3;&#x3002;<br>&#x2003;&#x2003;<strong>&#x7EBF;&#x7A0B;&#x7684;API&#x601D;&#x7EF4;&#x6765;&#x4F7F;&#x7528;&#x534F;&#x7A0B;&#xFF0C;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x7684;&#x6027;&#x80FD;&#x6765;&#x6D4B;&#x8BD5;&#x534F;&#x7A0B;&#x3002;</strong><br>&#x2003;&#x2003;NtyCo&#x5C01;&#x88C5;&#x51FA;&#x6765;&#x4E86;&#x82E5;&#x5E72;&#x63A5;&#x53E3;&#xFF0C;&#x4E00;&#x7C7B;&#x662F;&#x534F;&#x7A0B;&#x672C;&#x8EAB;&#x7684;&#xFF0C;&#x4E8C;&#x7C7B;&#x662F;posix&#x7684;&#x5F02;&#x6B65;&#x5C01;&#x88C5;<br>&#x2003;&#x2003;&#x534F;&#x7A0B;API&#xFF1A;  </p>
<ol>
<li>&#x534F;&#x7A0B;&#x521B;&#x5EFA;  <pre><code>int nty_coroutine_create(nty_coroutine **new_co, proc_coroutine func, void *arg)
</code></pre></li>
<li>&#x534F;&#x7A0B;&#x8C03;&#x5EA6;&#x5668;&#x7684;&#x8FD0;&#x884C;  <pre><code>void nty_schedule_run(void)
</code></pre></li>
<li>POSIX&#x5F02;&#x6B65;&#x5C01;&#x88C5;API&#xFF1A;  <pre><code>int nty_socket(int domain, int type, int protocol)
int nty_accept(int fd, struct sockaddr *addr, socklen_t *len)
int nty_recv(int fd, void *buf, int length)
int nty_send(int fd, const void *buf, int length)
int nty_close(int fd)
</code></pre>&#x63A5;&#x53E3;&#x683C;&#x5F0F;&#x4E0E;POSIX&#x6807;&#x51C6;&#x7684;&#x51FD;&#x6570;&#x5B9A;&#x4E49;&#x4E00;&#x81F4;&#x3002;</li>
</ol>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../chapter1/1_0.html" class="navigation navigation-prev " aria-label="Previous page: 协程的起源"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../chapter3/3_0.html" class="navigation navigation-next " aria-label="Next page: 协程的实现之工作原理"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-livereload/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"theme-default":{"showLevel":false},"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"livereload":{}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
